package org.alala.datamodels.common.query;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

public class QueryItem implements Serializable{
	
	//+工厂方法
	
	public static QueryItem newString(String fieldName) {
		QueryItem item = new QueryItem();
		item.setFieldName(fieldName);
		item.setDataType(DataTypeDef.STRING);
		
		return item;
	}
	
	public static QueryItem newNumber(String fieldName) {
		QueryItem item = new QueryItem();
		item.setFieldName(fieldName);
		item.setDataType(DataTypeDef.NUMBER);
		
		return item;
	}
	
	public static QueryItem newDate(String fieldName) {
		QueryItem item = new QueryItem();
		item.setFieldName(fieldName);
		item.setDataType(DataTypeDef.DATE);
		
		return item;
	}
	
	//-
	
	//+便捷方法
	
	public <T> QueryItem is(T value) {
		this.setValue(value);
		this.setCompareMethod(CompareMethodDef.EQUAL);
		return this;
	}
	
	public <T> QueryItem not(T value) {
		this.setValue(value);
		this.setCompareMethod(CompareMethodDef.NOT_EQUAL);
		return this;
	}
	
	public <T> QueryItem contains(T value) {
		this.setValue(value);
		this.setCompareMethod(CompareMethodDef.CONTAINS);
		return this;
	}

	public <T> QueryItem startsWith(T value) {
		this.setValue(value);
		this.setCompareMethod(CompareMethodDef.START_WITH);
		return this;
	}
	
	public <T> QueryItem endsWith(T value) {
		this.setValue(value);
		this.setCompareMethod(CompareMethodDef.END_WITH);
		return this;
	}
	
	@SuppressWarnings("unchecked")
	public <T> QueryItem in(T... values) {
		List<T> value = Arrays.asList(values);
		this.setValue(value);
		this.setCompareMethod(CompareMethodDef.IN);
		return this;
	}
	
	public <T> QueryItem in(Collection<T> values) {
		this.setValue(values);
		this.setCompareMethod(CompareMethodDef.IN);
		return this;
	}
	
	public <T> QueryItem gt(T value) {
		this.setValue(value);
		this.setCompareMethod(CompareMethodDef.LARGER);
		return this;
	}
	
	public <T> QueryItem gte(T value) {
		this.setValue(value);
		this.setCompareMethod(CompareMethodDef.LARGER_EQUAL);
		return this;
	}
	
	public <T> QueryItem lt(T value) {
		this.setValue(value);
		this.setCompareMethod(CompareMethodDef.LESS);
		return this;
	}
	
	public <T> QueryItem lte(T value) {
		this.setValue(value);
		this.setCompareMethod(CompareMethodDef.LESS_EQUAL);
		return this;
	}
	
	//-

	/**
	 * 
	 */
	private static final long serialVersionUID = -4663405286781448312L;
	
	private String fieldName;
	
	private int dataType;
	
	private Object value;
	
	private String compareMethod;
	
	/**
	 * Constructor
	 * 
	 */
	public QueryItem(String fieldName, int dataType, Object value, String compareMethod){
		this.fieldName = fieldName;
		this.dataType = dataType;
		this.value = value;
		this.compareMethod = compareMethod;
	}
	
	public QueryItem(){
		
	}
	

	public Object getValue() {

		return value;
	}

	public void setValue(Object value) {

		this.value = value;
	}

	public String getCompareMethod() {

		return compareMethod;
	}

	public void setCompareMethod(String compareMethod) {

		this.compareMethod = compareMethod;
	}

	public int getDataType() {

		return dataType;
	}

	public void setDataType(int dataType) {

		this.dataType = dataType;
	}

	public String getFieldName() {

		return fieldName;
	}

	public void setFieldName(String fieldName) {

		this.fieldName = fieldName;
	}
	
	/**
	 * 检查此对象的数据是否有效
	 * @return
	 */
	public boolean isValid() {
		return StringUtils.isNoneBlank(this.fieldName) && this.dataType >= 1 && this.dataType <= 3
				&& StringUtils.isNoneBlank(this.compareMethod) && CompareMethodDef.isValid(compareMethod);
	}
}
